package com.example.yuanboot.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ArrayUtil;
import com.example.yuanboot.common.msg.BaseResultCode;
import com.example.yuanboot.common.msg.ObjectRestResponse;
import com.example.yuanboot.common.msg.ResponseUtils;
import com.example.yuanboot.model.dto.TestDto;
import com.google.common.collect.Lists;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author qingyan 2021-06-04
 */
@Controller
@RequestMapping("test")
public class HtmlController {

    @GetMapping("/to")
    public String index(String path) {
         return Base64.decodeStr(path);
    }

    @GetMapping("/")
    public String index(){
        return "index";
    }

    @GetMapping("/test")
    public String test(){
        return "index";
    }

    @GetMapping("/test2")
    @ResponseBody
    public String test2(){
        return "index";
    }

    @PostMapping("/test")
    public ObjectRestResponse test(@Validated @RequestBody TestDto dto){
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);

    }


    /**
     * 参数后面不能使用bingResult、errors，bingdingResult、errors只能使用在pojo后面
     * 只能使用@Validated，不能使用@Valid，使用@Valid失效
     * @Validated只能标注在类上，标注在参数体、参数所在方法上注解失效
     * 需要自定义异常处理对检验异常进行处理
     * 校验注解只能在requestparam，pathvariable后面
     */
    @PostMapping("/single")
    public ObjectRestResponse test(@RequestParam(value = "dto") @Length(min = 10,max = 20) String dto){
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);
    }

    @PostMapping("/path/{code}")
    public ObjectRestResponse path(@PathVariable(value = "code") @NotBlank(message = "不能为空啊") String code){
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);
    }

    @GetMapping("/test1")
    @ResponseBody
    public ObjectRestResponse test1(@NotNull Integer params) {
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);
    }


    /**
     * 对于集合在controller的参数校验
     * 必须使用Validated和Valid
     * 其中Validated在类上，Valid在参数上且在RequestBody和RequestParam后面
     */
    @PostMapping("/list")
    public ObjectRestResponse list(@RequestBody @Valid List<TestDto> dtoList){

        LinkedHashSet<String> set = new LinkedHashSet<>();
//        set.iterator();
//        set.forEach();
//        new HashMap<>().forEach();
//        Lists.newArrayList().forEach();
//        Lists.newArrayList().iterator();
        for (Object o : Lists.newArrayList()) {

        }
        for (int i = 0; i < Lists.newArrayList().size(); i++) {

        }
        return ResponseUtils.r(null, BaseResultCode.SUCCESS);
    }


}
